package com.wk.controller;

import com.wk.dao.CityRepository;
import com.wk.entity.City;
import com.wk.entity.User;
import com.wk.myannotation.DoneTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private MessageSource ms;

    @PostMapping(value = "/Valid")
    public Map<String,String> index(@Valid User user, BindingResult bindingResult)
    {
        Map<String,String>  map = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        if(bindingResult.hasErrors())
        {
            List<ObjectError> list = bindingResult.getAllErrors();
            for (ObjectError error:list)
            {
                sb.append(error.getDefaultMessage());
                sb.append("----");
            }
            map.put("error",sb.toString());
            map.put("user",user.toString());
        }

        return map;
    }

    @GetMapping("/")
    public String index(Model model){
        Locale locale = LocaleContextHolder.getLocale();
        model.addAttribute("message",ms.getMessage("message",new String[]{"Java","WANGKUN"},locale));
        return "index";
    }

    @GetMapping(value = "/test")
    public String testError() {
        String na = null;
        return na.toString();
    }

    @Autowired
    private CityRepository cityRepository;


    //http://localhost:8080/saveCity?cityName=北京&cityIntroduce=中国首都
    @GetMapping(value = "saveCity")
    public String saveCity(String cityName,String cityIntroduce){
        City city = new City(cityName,cityIntroduce);
        cityRepository.save(city);
        return "success";
    }

    //http://localhost:8080/deleteCity?cityId=2
    @GetMapping(value = "deleteCity")
    public String deleteCity(int cityId){
        cityRepository.deleteById(cityId);
        return "success";
    }

    //http://localhost:8080/updateCity?cityId=3&cityName=沈阳&cityIntroduce=辽宁省省会
    @GetMapping(value = "updateCity")
    public String updateCity(int cityId,String cityName,String cityIntroduce){
        City city = new City(cityId,cityName,cityIntroduce);
        cityRepository.save(city);
        return "success";
    }

    //http://localhost:8080/getCityById?cityId=3
    @GetMapping(value = "getCityById")
    @Cacheable(value = "city",key = "#cityId")
    @DoneTime(param = "has work?")
    public City getCityById(int cityId){
        City city = cityRepository.findById(cityId).get();
        return city;
    }
}
